package cn.csust.net2.manager.client.ux;

import java.util.ArrayList;
import java.util.List;

import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.service.BaseServiceAsync;

import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelReader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Record;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.StoreEvent;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridView;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class BeanGrid<T extends PO> extends Grid<BeanModel> implements
		Listener<GridEvent<BeanModel>> {
	protected BaseServiceAsync service;
	/**
	 * BEANGRID的远程代理beanGrid b = new BeanGrid(); b.store.add();
	 */
	private RpcProxy<PagingLoadResult<T>> beanProxy;
	private BasePagingLoader<PagingLoadResult<ModelData>> beanLoader = null;
	private PagingToolBar beanToolBar;
	private ListStore<BeanModel> beanStore;
	// private BeanServiceAsync<T> service = GWT.create(BeanService.class);

	private int currentRow;
	private boolean isDatabase = false;
	private final PO po;

	private List<Record> modifyRecord = new ArrayList<Record>();

	public BeanGrid(final PO po, BaseServiceAsync service) {

		super();
		this.po = po;
		this.service = service;
		this.beanProxy = new RpcProxy<PagingLoadResult<T>>() {

			@Override
			protected void load(Object loadConfig,
					AsyncCallback<PagingLoadResult<T>> callback) {
				// TODO Auto-generated method st
				BeanGrid.this.service.findAll(po.getAssist().getClassName(),
						(PagingLoadConfig) loadConfig, callback);
			}
		};
		this.beanLoader = new BasePagingLoader<PagingLoadResult<ModelData>>(
				beanProxy, new BeanModelReader());
		this.beanLoader.setRemoteSort(true);
		beanStore = new ListStore<BeanModel>(beanLoader);
		this.store = beanStore;
		this.beanToolBar = new PagingToolBar(3);
		this.beanToolBar.bind(this.beanLoader);

		this.cm = ColumnFactory.createColumn(po, service);
		this.view = new GridView();
		disabledStyle = null;
		baseStyle = "x-grid-panel";
		setSelectionModel(new CheckBoxSelectionModel<BeanModel>());
		disableTextSelection(true);
		this.setLoadMask(true);
		this.setBorders(true);
		this.addListener(Events.Attach, this);

		this.store.addListener(Store.Add, new StoreListener());
		this.store.addListener(Store.Update, new StoreListener());
		this.store.addListener(Store.Remove, new StoreListener());

		this.addListener(Events.RowClick, new Listener<GridEvent<BeanModel>>() {

			@Override
			public void handleEvent(GridEvent<BeanModel> be) {
				// TODO Auto-generated method stub
				currentRow = be.getRowIndex();
			}

		});
	}

	/**
	 * 内部类 提供BEANGRID的数据处理的监听类
	 * 
	 * @author 韩忠金
	 * 
	 */
	public class StoreListener implements Listener<StoreEvent<BeanModel>> {

		@Override
		public void handleEvent(StoreEvent<BeanModel> be) {
			// 添加数据情况
			if (be.getType().equals(Store.Add)) {

			} else if (be.getType().equals(Store.Remove)) {
				System.out.println("删除成功");
			} else if (be.getType().equals(Store.Update)) {
				if (isDatabase) {
					// T t = beanStore.getAt(currentRow).getBean();
					List<PO> list = turnRecordToT();
					BeanGrid.this.service.update(list,
							new AsyncCallback<Void>() {

								@Override
								public void onFailure(Throwable caught) {

									Info.display("系统信息", "Update操作失败");
								}

								@Override
								public void onSuccess(Void result) {
									BasePagingLoadConfig configs = (BasePagingLoadConfig) BeanGrid.this.getBeanLoader().getLastConfig();
									BeanGrid.this.beanLoader.load(configs);
									setModifyRecord(new ArrayList<Record>());
									Info.display("系统信息", "Update操作成功");

								}
							});
				}

			}
		}
	}

	public List<PO> turnRecordToT() {
		List<PO> list = new ArrayList<PO>();
		for (Record record : modifyRecord) {
			T t = ((BeanModel) record.getModel()).getBean();
			list.add(t);
		}
		return list;
	}

	public RpcProxy<PagingLoadResult<T>> getBeanProxy() {
		return beanProxy;
	}

	public void setBeanProxy(RpcProxy<PagingLoadResult<T>> beanProxy) {
		this.beanProxy = beanProxy;
	}

	public PagingToolBar getBeanToolBar() {
		return beanToolBar;
	}

	public void setBeanToolBar(PagingToolBar beanToolBar) {
		this.beanToolBar = beanToolBar;
	}

	public BasePagingLoader<PagingLoadResult<ModelData>> getBeanLoader() {
		return beanLoader;
	}

	public int getCurrentRow() {
		return currentRow;
	}

	public void setCurrentRow(int currentRow) {
		this.currentRow = currentRow;
	}

	@Override
	public void handleEvent(GridEvent<BeanModel> be) {
		// TODO Auto-generated method stub

	}
	public List<Record> getModifyRecord() {
		return modifyRecord;
	}

	public void setModifyRecord(List<Record> modifyRecord) {
		this.modifyRecord = modifyRecord;
	}

	public boolean isDatabase() {
		return isDatabase;
	}

	public void setDatabase(boolean isDatabase) {
		this.isDatabase = isDatabase;
	}

	public BaseServiceAsync getService() {
		return service;
	}

	public void setService(BaseServiceAsync service) {
		this.service = service;
	}

	public ListStore<BeanModel> getBeanStore() {
		return beanStore;
	}

	public void setBeanStore(ListStore<BeanModel> beanStore) {
		this.beanStore = beanStore;
	}

	public PO getPo() {
		return po;
	}

	public void setBeanLoader(
			BasePagingLoader<PagingLoadResult<ModelData>> beanLoader) {
		this.beanLoader = beanLoader;
	}

}
